package com.tobias.gaokao.controller;

import com.tobias.gaokao.interceptor.RateLimiter;
import com.tobias.gaokao.service.ScoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tobias.gaokao.constants.PTConstants;
import com.tobias.gaokao.constants.Result;
import com.tobias.gaokao.constants.ResultEnum;
import com.tobias.gaokao.constants.ResultUtil;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author TobiasWang 2020/11/16
 */

@RestController
@RequestMapping(value = "/score")
public class ScoreController {
	@Resource
	private ScoreService scoreService;

	static final Logger logger = LoggerFactory.getLogger(ScoreController.class);

	@PostMapping(value = "/search")
	@RateLimiter(LimitNum = 30)
	public Result getOderTable(@RequestBody(required = true) String rawData, HttpSession session) {

		logger.info("-->Start to search student score!");
		if (null == session.getAttribute(PTConstants.USER_NAME)) {
			return ResultUtil.error(ResultEnum.USER_LOGOUT);
		}
		if(!rawData.split("=")[0].equalsIgnoreCase((String) session.getAttribute(PTConstants.UNIQUE_NUMBER))) {
			return ResultUtil.error(ResultEnum.PERMISSION_DENNY);
		}
		return scoreService.getScore(rawData.split("=")[0], session);
	}

}